import statEnum from "../enum/stat.enum"
class FpsCalculator {
  perfStatInterval: number
  elapsedTime: number
  frameCount: number
  lastCalcTime: number
  fps: number[]
  waitForUpdate: any
  constructor(e, t) {
    this.perfStatInterval = 1000
    this.elapsedTime = 0
    this.frameCount = 0
    this.lastCalcTime = 0
    this.fps = [0]
    this.waitForUpdate = e
    this.perfStatInterval = t.tryGetProperty("perfInterval", this.perfStatInterval)
  }
  async calcAndBroadcast(e) {
    await this.waitForUpdate()
    const t = Date.now(),
      o = t - this.lastCalcTime
    this.lastCalcTime = t
    this.elapsedTime += o
    this.frameCount++,
      this.elapsedTime >= this.perfStatInterval &&
        ((this.fps[0] = (1000 * this.frameCount) / this.elapsedTime), (this.elapsedTime = 0), (this.frameCount = 0), e.broadcast(statEnum.FPS, () => this.fps)),
      this.calcAndBroadcast(e)
  }
}
export default {
  Event: sdk => {
    sdk.addEnumToInterface({ namespace: "Stat", name: "Event", values: statEnum })
  },
  onEvent: (sdk, allFunc, dependencies) => {
    dependencies.getDependencies().then(([o]) => {
      new FpsCalculator(allFunc.waitForUpdate, o).calcAndBroadcast(sdk)
    })
  }
}
